package com.wechat.page.chat;

import com.swak.fx.support.AbstractPage;
import com.swak.fx.support.FXMLView;
import com.swak.fx.support.ui.UI;
import com.swak.fx.support.ui.UIIcon;
import com.wechat.dto.ChatDTO;
import com.wechat.page.chat.content.ChattingPage;
import com.wechat.page.chat.title.ChatTitleComp;

import javafx.beans.property.LongProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;

/**
 * ChatContentPage.
 * 
 * @author 618lf
 */
@FXMLView(title = "聊天内容", value = "ChatContentPage.fxml", css = "ChatContentPage.css")
public class ChatContentPage extends AbstractPage {

	private ChatTitleComp titlePage;
	
	@FXML
	private StackPane contentPane;
	private UIIcon chatIcon;

	private ChattingPage chattingPage;

	public ChatContentPage() {
		this.titlePage = this.NewComp(ChatTitleComp.class);
		this.chatIcon = new UIIcon(UI.Icons.COMMENTS);
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		Node titlePane = titlePage.getRoot();
		this.addChild(titlePane);
		GridPane.setConstraints(titlePane, 0, 0);
		GridPane.setHgrow(titlePane, Priority.ALWAYS);

		this.chatIcon.Size(88);
		this.chatIcon.setOpacity(0.2);
		this.contentPane.getChildren().add(this.chatIcon);
	}

	@Override
	public void setupEventHandlers() {
		super.setupEventHandlers();
	}

	@Override
	public void setupBindings() {
		super.setupBindings();
		this.titlePage.chatProperty().bind(this.activeProperty());
		this.titlePage.refreshProperty().bind(this.refreshProperty());
		this.root.setOnDragOver((e) -> {
			e.acceptTransferModes(TransferMode.ANY);
		});
		this.root.setOnDragDropped((e) -> {
			TransferMode mode = e.getAcceptedTransferMode();
			if (mode == TransferMode.MOVE) {
			}
		});
	}

	@Override
	public void setupValueChangedListeners() {
		super.setupValueChangedListeners();
		this.activeProperty().addListener(this::onChatActive);
	}

	private void onChatActive(ObservableValue<? extends ChatDTO> ob, ChatDTO unActive, ChatDTO active) {
		this.$runUI(() -> {
			ChattingPage page = this.newChattingPage();
			if (active != null) {
				this.contentPane.getChildren().remove(0);
				this.contentPane.getChildren().add(page.getRoot());
				page.chatProperty().set(active);
			} else {
				this.contentPane.getChildren().remove(0);
				this.contentPane.getChildren().add(chatIcon);
			}
		});
	}

	private ChattingPage newChattingPage() {
		if (this.chattingPage == null) {
			synchronized (this) {
				if (this.chattingPage == null) {
					this.chattingPage = this.NewPage(ChattingPage.class);
				}
			}
		}
		return this.chattingPage;
	}

	@Override
	public void windowStateChange(int state) {
		super.windowStateChange(state);
		if (UI.States.isWindowShow(state)) {
			this.newChattingPage();
		}
	}

	// ================================================================================
	// Properties
	// ================================================================================
	private final ObjectProperty<ChatDTO> activeProperty = new SimpleObjectProperty<>();

	public ObjectProperty<ChatDTO> activeProperty() {
		return activeProperty;
	}

	private final LongProperty refreshProperty = new SimpleLongProperty();

	public LongProperty refreshProperty() {
		return refreshProperty;
	}
}
